<?php
/**
 * Created by PhpStorm.
 * User: zyark
 * Date: 2017/5/27
 * Time: 17:25
 */

namespace Home\Logic;


use Common\Utils\ImageUtils;
use Home\Model\Page;
use Think\Image;

class CommentLogic
{

    /**
     * 分页获取商品评论列表
     * @param $goods_id 商品id
     * @param $type 评论类型 0好评 1中评 2差评
     * @param int $pagenum 页数
     * @param int $pagesize 每页显示数据数量
     * @return 评论列表
     */
    public function getCommentList($goods_id,$type,$pagenum=1,$pagesize=20){
        $startno = ($pagenum - 1) * $pagesize;
        $limit = $startno . ',' . $pagesize;//每页的数据数和内容$limit
        $map['tp_comment.goods_id'] = ':goods_id';
        $bind[':goods_id'] = $goods_id;
        $map['tp_comment.type'] = ':type';
        $bind[':type'] = $type;
        $commentList = D('Comment')
            ->join('LEFT JOIN tp_users u on u.user_id=tp_comment.user_id')
            ->where($map)->bind($bind)
            ->limit($limit)
            ->field('tp_comment.*,u.head_pic as image_url')
            ->order('comment_id desc')
            ->select();
        foreach ($commentList as $key=>$value){
            $comment_id = $commentList[$key]['comment_id'];
            $commentList[$key]['add_time'] = date("Y年m月d日 H:i", $commentList[$key]['add_time']);
            $image_urls = D('CommentImage')->where(array('comment_id'=>$comment_id))->select();

//            foreach($image_urls as $k=>$v){
//                $utils = new ImageUtils();
//                $image_urls[$k]['thumb_image_url'] = $utils->thum_images($v['id'],220,220,'comment_images',$v['image_url']);
//            }
            $commentList[$key]['image_urls'] = $image_urls;
        }
        $data['list'] = $commentList;
        $count = D('Comment')->where($map)->bind($bind)->count();
        $page = new Page($pagenum, $count, $pagesize);
        $page = (array)$page;//转换为数组
        //分页数据
//        $page['cpage'] = $pagenum;//当前页面
//        $page['totalnum'] = floor(($count + $pagesize - 1) / $pagesize);
        $data['page'] = $page;

        return $data;
    }

}